package com.example.demo.zhoushengwen.configurer;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebFilter(filterName = "bodyReaderFilter", urlPatterns = "/*")
public class BodyReaderFilter implements Filter
{
    
    @Override
    public void init(FilterConfig filterConfig)
        throws ServletException
    {
        // do nothing
    }
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException
    {
        log.info("过滤器执行开始");
        ServletRequest requestWrapper = null;
        if (request instanceof HttpServletRequest)
        {
            requestWrapper = new BodyReaderHttpServletRequestWrapper((HttpServletRequest)request);
        }
        if (requestWrapper == null)
        {
            chain.doFilter(request, response);
        }
        else
        {
            chain.doFilter(requestWrapper, response);
        }
        log.info("过滤器执行结束");
    }
    
    @Override
    public void destroy()
    {
        // do nothing
        
    }
    
}
